package com.example.tool.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.example.tool.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liangkun on 2018/8/11.
 * 作用:运动统计数据
 */

public class BarRoundWeekView extends View {
    private Paint mPaint, mChartPaint;
    private Rect mBound;
    private int mHeight, mWidth, mChartWidth, mSize;
    private List<Float> list = new ArrayList<>();
    private getNumberListener listener;
    private int selectIndex = -1;
    private List<RectF> rectFList = new ArrayList<>();
    private float centerX;
    private float top;
    private int commonBarColor;
    private int selectBarColor;
    private int bottomTextColor;
    private float bottomTextSize;
    private float barTextSpace;
    private Context context;

    /**
     * 设置数据
     *
     * @param list         数据
     * @param compareIndex 占比
     */
    public void setList(List<Float> list, int compareIndex) {
        this.list = list;
        selectIndex = compareIndex;
        invalidate();
    }

    public BarRoundWeekView(Context context) {
        this(context, null);
    }

    public BarRoundWeekView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BarRoundWeekView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BarRoundChartView, defStyleAttr, 0);
        commonBarColor = typeArray.getColor(R.styleable.BarRoundChartView_commonBarColor, Color.BLACK);
        selectBarColor = typeArray.getColor(R.styleable.BarRoundChartView_selectBarColor, Color.BLACK);
        bottomTextColor = typeArray.getColor(R.styleable.BarRoundChartView_bottomTextColor, Color.BLACK);
        bottomTextSize = typeArray.getDimension(R.styleable.BarRoundChartView_bottomTextSize, 35);
        barTextSpace = typeArray.getDimension(R.styleable.BarRoundChartView_barTextSpace, 50);
        this.context = context;
        typeArray.recycle();
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width;
        int height;
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = widthSize * 1 / 2;
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = heightSize * 1 / 2;
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = getWidth();
        mHeight = getHeight();

    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mBound = new Rect();
        mChartPaint = new Paint();
        mChartPaint.setAntiAlias(true);
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (hasWindowFocus) {

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        rectFList.clear();
        mSize = getWidth() / 45; //控制柱状图的宽度和间距
//        mChartWidth = getWidth() / 20 - mSize / 2; //bar的起点
        mChartWidth = mSize; //bar的起点

//        canvas.drawLine(mChartWidth, mHeight - barTextSpace, getWidth() - mSize, mHeight - barTextSpace, mPaint);
        canvas.drawLine(mChartWidth, mHeight - barTextSpace, getWidth()-mChartWidth , mHeight - barTextSpace, mPaint);
        for (int i = 0; i < 21; i++) {
            RectF rectF = null;
            int size = mHeight / 120;
            mChartPaint.setStyle(Paint.Style.FILL);
            if (list.size() > 0) {
//                if (selectIndexRoles.contains(i)) {
                if (selectIndex == i) {
                    //选中的view
                    mChartPaint.setShader(null);
                    mChartPaint.setColor(selectBarColor);
                } else {
                    //普通的view
                    mChartPaint.setColor(commonBarColor);
                }
                //画柱状图
                rectF = new RectF();
                rectF.left = mChartWidth;
                rectF.right = mChartWidth + mSize;
//                rectF.bottom = mHeight - 50;
                rectF.bottom = mHeight - barTextSpace;
                //最大值是120*5
//                rectF.top = mHeight-120*5 ;
                rectF.top = rectF.bottom - list.get(i) * size;
                canvas.drawRoundRect(rectF, 20, 20, mChartPaint);
//                }
                //画数字
                if (i == 0 || i == 10 || i == 20) {
                    mPaint.setColor(bottomTextColor);
                    mPaint.setTextSize(bottomTextSize);
                    mPaint.setTextAlign(Paint.Align.CENTER);
                    mPaint.getTextBounds(String.valueOf(i) + "", 0, String.valueOf(i).length(), mBound);
                    canvas.drawText(String.valueOf(i), rectF.centerX(),
                            mHeight - barTextSpace / 2 + mBound.height() * 0.5f, mPaint);

                }
                mChartWidth += getWidth() / 20-context.getResources().getDimension(R.dimen.sw_dp_1);

            }
            if (selectIndex == i) {
                //坐标
                centerX = rectF.centerX();
                top = rectF.top;
                listener.getNumber(i, centerX, top);
            }
            //非空
            if (rectF != null) {
                rectFList.add(rectF);
            }
        }

    }
 //TODO:点击操作的数据
//    @Override
//    public boolean onTouchEvent(MotionEvent ev) {
//
//        int x = (int) ev.getX();
//        int y = (int) ev.getY();
//        switch (ev.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                for (int i = 0; i < 20; i++) {
//                    RectF rectF = rectFList.get(i);
//                    if (rectF.contains(x, y)) {
//
//                        if (listener != null) {
//                            //坐标
//                            centerX = rectF.centerX();
//                            top = rectF.top;
//                            listener.getNumber(i, centerX, top);
//                            selectIndex = i;
//                            selectIndexRoles.clear();
//                            selectIndexRoles.add(selectIndex);
//                            postInvalidate();
//                        }
//                    }
//
//                }
//                break;
//            case MotionEvent.ACTION_UP:
//
//                break;
//        }
//        return true;
//    }

    public void setListener(getNumberListener listener) {
        this.listener = listener;
    }

    public interface getNumberListener {
        void getNumber(int number, float x, float y);
    }

}